package com.graduation.springbootinit.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.graduation.springbootinit.common.BaseResponse;
import com.graduation.springbootinit.common.ErrorCode;
import com.graduation.springbootinit.common.ResultUtils;
import com.graduation.springbootinit.exception.BusinessException;
import com.graduation.springbootinit.model.dto.product.ProductQueryRequest;
import com.graduation.springbootinit.model.dto.product.StoreProductQueryRequest;
import com.graduation.springbootinit.model.dto.product.UpdateSizeStockRequest;
import com.graduation.springbootinit.model.entity.ShopPart.ClothingProduct;
import com.graduation.springbootinit.model.entity.ShopPart.SizeStock;
import com.graduation.springbootinit.service.ClothingProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "商品接口")
@RestController
@RequestMapping("/api/shopPart/product")
public class ClothingProductController {

    @Resource
    private ClothingProductService productService;

    @ApiOperation("创建商品")
    @PostMapping("/create")
    public BaseResponse<Long> createProduct(@RequestBody ClothingProduct product) {
        Long productId = productService.createProduct(product);
        return ResultUtils.success(productId);
    }

    @ApiOperation("更新商品")
    @PostMapping("/update")
    public BaseResponse<Boolean> updateProduct(@RequestBody ClothingProduct product) {
        productService.updateProduct(product);
        return ResultUtils.success(true);
    }

    @ApiOperation("删除商品")
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteProduct(@ApiParam("商品ID") @RequestParam Long id) {
        productService.deleteProduct(id);
        return ResultUtils.success(true);
    }

    @ApiOperation("获取商品详情")
    @GetMapping("/get")
    public BaseResponse<ClothingProduct> getProduct(@ApiParam("商品ID") @RequestParam Long id) {
        ClothingProduct product = productService.getProductById(id);
        return ResultUtils.success(product);
    }

    @ApiOperation("获取商品库存详情")
    @GetMapping("/getsizeStocks")
    public BaseResponse<List<SizeStock>> getProductsizeStocks(@ApiParam("商品ID") @RequestParam Long id) {
        ClothingProduct product = productService.getProductById(id);
        List<SizeStock> sizeStocks = product.getSizeStocks();
        return ResultUtils.success(sizeStocks);
    }


    @ApiOperation("分页获取商品列表")
    @PostMapping("/list")
    public BaseResponse<Page<ClothingProduct>> listProducts(@RequestBody ProductQueryRequest productQueryRequest) {
        Page<ClothingProduct> page = productService.listProducts(
                new Page<>(productQueryRequest.getCurrent(), productQueryRequest.getPageSize()),
                productQueryRequest.getKeyword(),
                productQueryRequest.getCategoryId(),
                productQueryRequest.getSortField(),
                productQueryRequest.getSortOrder(),
                productQueryRequest.getMinPrice(),
                productQueryRequest.getMaxPrice()
        );
        return ResultUtils.success(page);
    }

    @ApiOperation("更新商品尺码库存")
    @PostMapping("/sizeStock/update")
    public BaseResponse<Boolean> updateSizeStock(@RequestBody UpdateSizeStockRequest request) {
        productService.updateSizeStock(request.getId(), request.getSizeStocks());
        return ResultUtils.success(true);
    }

    @ApiOperation("获取店铺所有商品详情")
    @PostMapping("/store/products")
    public BaseResponse<Page<ClothingProduct>> getStoreProducts(@RequestBody StoreProductQueryRequest request) {
        if (request == null || request.getStoreId() == null || request.getStoreId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Page<ClothingProduct> page = productService.listStoreProducts(
                new Page<>(request.getCurrent(), request.getPageSize()),
                request.getStoreId()
        );
        return ResultUtils.success(page);
    }

    @ApiOperation("分页获取店铺商品列表")
    @PostMapping("/store/list")
    public BaseResponse<Page<ClothingProduct>> listStoreProducts(@RequestBody StoreProductQueryRequest request) {
        Page<ClothingProduct> page = productService.listStoreProducts(
                new Page<>(request.getCurrent(), request.getPageSize()),
                request.getStoreId()
        );
        return ResultUtils.success(page);
    }

    @ApiOperation("检查库存")
    @GetMapping("/{id}/stock")
    public BaseResponse<Boolean> checkStock(
            @PathVariable Long id,
            @RequestParam String size,
            @RequestParam Integer quantity) {
        return ResultUtils.success(productService.decreaseStock(id, size, quantity));
    }
}